package com.ruoyi.project.system.device.pdf;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
 
public class PDFDemo {
    public static void main(String[] args) {
        demoFile("这个是水印");
    }
 
    /**
     * 文件生成主方法
     * @param aligned
     * @return
     */
    public static String demoFile(String aligned){
        // 上传文件路径
        String fileName = PDFExport.extractFilename();
        String absPath = PDFExport.getAbsoluteFile(fileName).getAbsolutePath();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        InputStream inputStream;
        PdfReader reader;
        PdfStamper ps;
        try {
            inputStream = PDFExport.getTpl().getInputStream();
            reader = new PdfReader(inputStream);
            ps = new PdfStamper(reader, bos);
            ps.setFormFlattening(true);
            ps.close();//必须在这里进行close
            //*******************填充编辑好后的pdf**************
            reader = new PdfReader(bos.toByteArray());
            Rectangle pageSize = reader.getPageSize(1);
            Document document = new Document(pageSize);
            PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream(absPath));
            writer.setPageEvent(new PDFPageEvent(aligned));
            // 打开文档
            document.open();
            PdfContentByte cbUnder = writer.getDirectContentUnder();
            PdfImportedPage pageTemplate = writer.getImportedPage(reader, 1);
            cbUnder.addTemplate(pageTemplate, 0, 0);
            //添加间隙,这里为进行了一个封装，因为这个模板第一页只有一些title啥的，
            //重开一页太浪费，只需要确定表格要在什么位置生成，添加一个间隙就可以了
            //这里进行数据填充
            demoData(writer, document);
            // low level
            PdfContentByte cb = writer.getDirectContent();
            cb.fill();
            cb.sanityCheck();
            document.close();
            inputStream.close();
            reader.close();
            ps.close();
            return PDFExport.getPathFileName(fileName);
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        } catch (DocumentException e) {
            e.printStackTrace();
            return "";
        }
    }
 
    /**
     *
     * @param writer
     * @param document
     */
    public static void demoData(PdfWriter writer, Document document) throws IOException, DocumentException {
        //新创建一个7列的表格
        PdfPTable table = PDFExport.getTable(writer,new float[]{25,30,30,30,30,30,30});
        //表头信息
 
        //有7列数据将行全部合并  并且隐藏边框
        PDFExport.setRowData(table,1,7,"XXXX表",13);
 
        //有12行数据将第一列全部合并
        PDFExport.setRowData(table,12,1,"合并列明");
 
        PDFExport.setRowData(table,1,2,"XXXX表");
        PDFExport.setRowData(table,1,4,"-");
 
        PDFExport.setRowData(table,1,1,"时   间");
        PDFExport.setRowData(table,1,1,"-");
        PDFExport.setRowData(table,1,1,"时   间");
        PDFExport.setRowData(table,1,1,"-");
        PDFExport.setRowData(table,1,1,"时   间");
        PDFExport.setRowData(table,1,1,"-");
 
        PDFExport.setRowData(table,1,1,"时   间");
        PDFExport.setRowData(table,1,1,"-");
        PDFExport.setRowData(table,1,1,"时   间");
        PDFExport.setRowData(table,1,1, "-");
        PDFExport.setRowData(table,1,1,"时   间");
        PDFExport.setRowData(table,1,1,"-");
 
        PDFExport.setRowData(table,1,2,"时   间");
        PDFExport.setRowData(table,1,4,"-");
        PDFExport.setRowData(table,1,2,"时   间");
        PDFExport.setRowData(table,1,4,"-");
 
        PDFExport.setRowData(table,1,2,"时   间");
        PDFExport.setRowData(table,1,1,"-");
        PDFExport.setRowData(table,1,2,"时   间");
        PDFExport.setRowData(table,1,1,"-");
 
        PDFExport.setRowData(table,1,2,"时   间");
        PDFExport.setRowData(table,1,4,"-");
 
        PDFExport.setRowData(table,1,2,"时   间");
        PDFExport.setRowData(table,1,4,"-");
 
        PDFExport.setRowData(table,4,1,"时   间");
 
        PDFExport.setRowData(table,1,2,"时   间");
        PDFExport.setRowData(table,1,3,"-");
 
        PDFExport.setRowData(table,1,2,"时   间");
        PDFExport.setRowData(table,1,3,"-");
 
        PDFExport.setRowData(table,1,2,"时   间");
        PDFExport.setRowData(table,1,3,"-");
 
        PDFExport.setRowData(table,1,2,"时   间");
        PDFExport.setRowData(table,1,3,"-");
 
        PDFExport.setRowData(table,8,1,"时   间");
 
        PDFExport.setRowData(table,1,1,"时   间");
        PDFExport.setRowData(table,1,1,"时   间");
        PDFExport.setRowData(table,1,1,"时   间");
        PDFExport.setRowData(table,1,1,"时   间");
        PDFExport.setRowData(table,1,1,"时   间");
        PDFExport.setRowData(table,1,1,"时   间");
 
        PDFExport.setRowData(table,1,1,"时   间");
        PDFExport.setRowData(table,1,1,"-");
        PDFExport.setRowData(table,1,1,"-");
        PDFExport.setRowData(table,1,1,"-");
        PDFExport.setRowData(table,1,1,"-");
        PDFExport.setRowData(table,1,1, "-");
 
        PDFExport.setRowData(table,1,1,"时   间");
        PDFExport.setRowData(table,1,1,"-");
        PDFExport.setRowData(table,1,1,"-");
        PDFExport.setRowData(table,1,1,"-");
        PDFExport.setRowData(table,1,1,"-");
        PDFExport.setRowData(table,1,1,"-");
 
        PDFExport.setRowData(table,1,3,"时   间");
        PDFExport.setRowData(table,1,3,"-");
 
        PDFExport.setRowData(table,1,3,"时   间");
        PDFExport.setRowData(table,1,3,"-");
 
        PDFExport.setRowData(table,1,3,"时   间");
        PDFExport.setRowData(table,1,3,"-");
 
        PDFExport.setRowData(table,1,3,"时   间");
        PDFExport.setRowData(table,1,3,"-");
 
        PDFExport.setRowData(table,1,3,"时   间");
        PDFExport.setRowData(table,1,3,"-");
 
        document.add(table);
    }
}